//
//  KKMQTTManager.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/12/21.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "KKMQTTManager.h"
//#import <MQTTClient/MQTTSessionManager.h>

//@interface KKMQTTManager () <MQTTSessionManagerDelegate>
//
//@property (nonatomic, strong) MQTTSessionManager *sessionManager;
//
//@end

@implementation KKMQTTManager

SingletonM(KKMQTTManager, [self initMethod];)

-(void)initMethod {
    
}

/*
-(void)stop {
    [NNCDC removeObserver:self];
    [_sessionManager disconnectWithDisconnectHandler:^(NSError *error) {
        if (error) {
            LOG(@"error %@", error.description);
        }
    }];
    _sessionManager = nil;
}

-(void)start {
    _sessionManager = [[MQTTSessionManager alloc] init];
    _sessionManager.delegate = self;
    _sessionManager.subscriptions = [self allTopics];
    [_sessionManager connectTo:@"10.10.20.28"
                          port:1883
                           tls:NO
                     keepalive:120
                         clean:NO
                          auth:YES
                          user:@"iOS"
                          pass:@"xulongwu"
                          will:NO
                     willTopic:nil
                       willMsg:nil
                       willQos:0
                willRetainFlag:NO
                  withClientId:@"iOS-client"
                securityPolicy:nil
                  certificates:nil
                 protocolLevel:MQTTProtocolVersion311
                connectHandler:^(NSError *error) {
                      if (error) {
                          LOG(@"error %@", error.description);
                      }
                  }];
    
//    [NNCDC addObserver:self selector:@selector(deviceChanged:) name:kDeviceCountChangedNotification object:nil];
}

-(void)deviceChanged:(NSNotification *)notification {
    _sessionManager.subscriptions = [self allTopics];
    [_sessionManager connectToLast:^(NSError *error) {
        if (error) {
            LOG(@"error %@", error.description);
        }
    }];
}

-(NSDictionary *)allTopics {
    NSMutableDictionary *topics = [NSMutableDictionary dictionary];
    for (PHDevice *model in [GPHDeviceManager deviceList]) {
        NSString *key = [NSString stringWithFormat:@"/1/device/%@/notification",model.device_code];
        [topics setObject:@1 forKey:key];
    }
    LOG(@"topics %@",topics);
    return topics;
}

#pragma mark - MQTTSessionManagerDelegate
-(void)sessionManager:(MQTTSessionManager *)sessionManager didChangeState:(MQTTSessionManagerState)newState {
    LOG(@"%s newState %d topics %@",__func__, newState, sessionManager.effectiveSubscriptions);
}

-(void)sessionManager:(MQTTSessionManager *)sessionManager didDeliverMessage:(UInt16)msgID {
    LOG(@"%s msgID %d",__func__, msgID);
}

- (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {
    LOG(@"%s topic:%@ retained:%d dataLength:%d %@",__func__,topic, retained, (int)data.length, data);
}
*/
@end
